package com.wjs.CarolSecKill.vo;

import org.thymeleaf.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidClass implements ConstraintValidator<IsMobile,String> {
    private boolean required=false;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required=constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
           return checkMobile(s);
        }else {
            if(StringUtils.isEmpty(s)){
                return true;
            }else {
               return checkMobile(s);
            }
        }
    }


    public boolean checkMobile(String mobile){
        if(!StringUtils.isEmpty(mobile)){
            if(mobile.length()>11){
                return false;
            }else {
                return true;
            }
        }else {
            return false;
        }
    }

}
